package com.xszx.server.service;

import com.xszx.server.dto.Page;
import com.xszx.server.entity.Category;
import com.xszx.server.mapper.CategoryMapper;
import com.xszx.server.util.UuidUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.List;

@Service
public class CategoryService {
    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 获取分类列表
     * @param
     * @return
     */
    public List<Category> getAllCategorys(){
        return categoryMapper.findAll();
    }

    /**
     * 新增或者删除信息
     * @param category
     * @return
     */
    public String saveOrUpdate(Category category){
        if(StringUtils.isEmpty(category.getId())){
            category.setId(UuidUtil.getShortUuid());
            return (categoryMapper.insert(category)>0)?"添加成功":"添加失败";
        }else{
            return (categoryMapper.updateByPrimaryKey(category)>0)?"更新成功":"更新失败";
        }
    }

    /**
     * 物理删除分类
     * @param id
     * @return
     */
    public int del(String id){
        return categoryMapper.deleteByPrimaryKey(id);
    }
     /**
     * 获取单个分类
     * @return
     */
    public Category getCategory(String id){
        return categoryMapper.selectByPrimaryKey(id);
    }
}
